/*
 * channelserver.h
 *
 *  Created on: 2013. 7. 19.
 *      Author: jjaehuny
 */

#ifndef CHANNELSERVER_H_
#define CHANNELSERVER_H_

#include <Dispatcher/Static.h>
#include <Network/Network.h>


#include "../idl/Cache.h"

#include <string>

namespace CacheServer {

class ChannelServer : public Dispatcher::Session
{
public:
	ChannelServer();
	virtual ~ChannelServer();

public:
	template <class MSG> void SendMsg(const MSG& msg)
	{
		int size = msg.Size();
		if (size > 10000){
			LOG(DEV, "WARNING SEND MSGID: %d size : %d",  MSG::MSG_ID , size);
		}
		Dispatcher::ListenHandler<Session, Network::Packet>::SendMsg(this, msg);
	}


public:
	//init_state _init_state;
	int				GetChannelKey() { return _channelKey; }

private:
	int 			_channelKey;
};

struct Msg
{
public:
	static void SendFailUserInfo(ChannelServer * pChannelServer, uint32_t seq, CacheResult result, const char * strError);
	static void SendFailCharacterInfo(ChannelServer * pChannelServer, uint32_t seq, CacheResult result, const char * strError);
	static void SendFailFlushCharacterInfo(ChannelServer * pChannelServer, uint32_t seq, CacheResult result, const char * strError);
	static void SendFailFlushUserInfo(ChannelServer * pChannelServer, uint32_t seq, CacheResult result, const char * strError);
	static void SendFlushUserSlotInfo(ChannelServer * pChannelServer, uint32_t seq, uint32_t slotIndex, CacheResult result, const char * strError);

public:
	static void OnUserInfoSyn(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
	static void OnCharacterInfoSyn(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
	static void OnFlushCharacterInfoSyn(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
	static void OnFlushUserInfoSyn(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
	static void OnFlushUserSlotInfoSyn(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
	static void OnRemoveUser(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
	static void OnNewCharacterInfoSyn(Dispatcher::Session* pSession, ::Network::Packet * pPacket );

	static void OnTime(int seq);
};

struct init_state : public Dispatcher::Session::State
{
public:
	init_state()
	{
		AddCommand(UserInfoSyn::MSG_ID, Msg::OnUserInfoSyn);
		AddCommand(CharacterInfoSyn::MSG_ID, Msg::OnCharacterInfoSyn);
		AddCommand(FlushCharacterInfoSyn::MSG_ID, Msg::OnFlushCharacterInfoSyn);
		AddCommand(FlushUserInfoSyn::MSG_ID, Msg::OnFlushUserInfoSyn);
		AddCommand(FlushUserSlotInfoSyn::MSG_ID, Msg::OnFlushUserSlotInfoSyn);
		AddCommand(RemoveUserSyn::MSG_ID, Msg::OnRemoveUser);

		AddCommand(NewCharacterInfoSyn::MSG_ID, Msg::OnNewCharacterInfoSyn);
	}
};

} /* namespace cacheserver */
#endif /* CHANNELSERVER_H_ */
